package org.openFreely.utils;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;


public class ServerUtils {

    private static int port;
    private static int threadCount;
    private static String messageInfo;
    private static ChannelInitializer channelInitializer;

    public static void bootstrapServer(){
        NioEventLoopGroup group=new NioEventLoopGroup(threadCount);
        try {
            Channel channel=null;
            if (channelInitializer == null) {
                channel=new ServerBootstrap().group(group)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new ChannelInitializer<NioSocketChannel>() {
                            @Override
                            protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                                nioSocketChannel.pipeline().addLast(new StringDecoder());
                                nioSocketChannel.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
                                    @Override
                                    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
                                        System.out.println(messageInfo + s);
                                    }
                                });
                            }
                        }).bind(port).sync().channel();
            } else {
                channel=new ServerBootstrap().group(new NioEventLoopGroup(threadCount))
                        .channel(NioServerSocketChannel.class)
                        .childHandler(channelInitializer).bind(port).sync().channel();
            }
            channel.closeFuture().sync();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            group.shutdownGracefully();
        }
    }

    public static int getPort() {
        return port;
    }

    public static void setPort(int port) {
        ServerUtils.port = port;
    }

    public static int getThreadCount() {
        return threadCount;
    }

    public static void setThreadCount(int threadCount) {
        ServerUtils.threadCount = threadCount;
    }

    public static ChannelInitializer getChannelInitializer() {
        return channelInitializer;
    }

    public static void setChannelInitializer(ChannelInitializer channelInitializer) {
        ServerUtils.channelInitializer = channelInitializer;
    }

    public static String getMessageInfo() {
        return messageInfo;
    }

    public static void setMessageInfo(String messageInfo) {
        ServerUtils.messageInfo = messageInfo;
    }
}

